<?php
namespace app\user\controller;





use think\Db;
use think\Request;

class Buyers extends \think\Controller
{


    public function _initialize()
    {
       //模块权限判断
       if(session('?uid')){


        }else{


             $this->redirect('index/login/login');

        }


    }

  



    public function index()
    {




        return view();


    }


       public function indexs()
    {
        $uid = session('uid');
        $t = Db::table('user')->find($uid);
        $this->assign('t', $t);
        return view();
    }



     public function meinfo()
    {

        if (IS_AJAX()) {
            $s = input('param.');
            unset($s['id']);
            $where['ID'] = session('uid');
            $flag = Db::table('user')->where($where)->update($s);
            $json['code'] = $flag ? 200 : 400;
            $json['msg'] = $flag ? "资料修改成功" : "资料修改失败，请重试";

            return json($json);


        } else {


            $uid = session('uid');
            $user = Db::table('user')->find($uid);
            $this->assign('t', $user);
            $this->assign('address', $address);
            return view();


        }


    }


      public function password()
    {

        if(IS_AJAX()){


            $s = input('param.');
            if (!$s['pass'] || $s['password'] != $s['word']) {
                $json['msg'] = "请输入正确的密码";
                $json['code'] = 400;
            } else {

                $where['PassWord'] = md5($s['pass']);
                $where['ID'] = session('uid');

                $user = Db::table('user')->where($where)->find();


                if ($user) {
                    $json['code'] = 200;

                    $update['PassWord'] = md5($s['password']);
                    Db::table('user')->where(['ID' => session('uid')])->update($update);

                    $json['code'] = 200;

                    $json['url'] = url('user/buyers/index');

                     } else {

                    $json['msg'] = "您的旧密码错误";
                    $json['code'] = 400;
              }


                return json($json);

                }
            }

            return view();
        
         }





      public function info()
      {
      $data['uid']=session('uid');

      // $data['time']= date("Y-m-d H:i:s");
      
      $info=Db::table('user_info')->alias(['user_info'=>'i','user'=>'u'])
                                  ->join('user u','u.ID = i.uid')
                                  ->where($data)
                                  ->order('i.id desc')
                                  ->paginate(10);
        // 获取分页显示
      
         $page = $info->render();
       $this->assign('page', $page);
      $this->assign('info',$info); 
     return view();
    }




      public function delinfo(){
        if(IS_AJAX()){
              $s=input('param.');

             $where['id']=$s['id'];

              $data=Db::table('user_collect')->where($where)->delete();

              $json['code']=$data ? 200 : 400 ;

              $json['msg']=$data ? "删除成功" : "删除失败" ;


              return json($json);

        }

    }




     public function gps()
    {
        if (IS_AJAX()) {
            $s= input('param.');

             if(!$s['address'] || !$s['name'] || $s['tel']!=11){
              $json['code']==400;
              $json['msg']=="保存失败";
            }else{
                $data['AccountID'] = session('uid');
        
                $data['address']=$s['address'];
                // 'Email' => 'Email',
               $data['name']  =$s ['name'];
                $data['tel'] =$s['tel'];

        
            $gps = Db::table('user_address')->where($data)->insert();
            if($gps){

                 $json['code']==200;
              $json['msg']=="保存成功";

            }else{

                $json['code']==400;
              $json['msg']=="保存失败";

            }

            }
          
         
            return json($data);


        } else {

            // $uid = session('uid');
            // $user = Db::table('user')->find($uid);
            // $this->assign('t', $user);

            $where['AccountID'] = session('uid');
            $info = Db::table('user_address')->where($where)->select();

            $this->assign('info', $info);

            return view();


        }


    }


      public function gpssave(){
       

            $s=input('param.');

            $where['id']=$s['id'];

            $where['AccountID']=session('uid');
              
            $info=Db::table('user_address')->where($where)->find();


           $this->assign('t',$info);



          $flag=Db::table('user_address')->where($where)->update($s);


                    
                 return view();   
        }



         public function save(){
            if(IS_AJAX()){
              $s=input('param.');
              $where['id']=$s['id'];

              $flag=Db::table('user_address')->where($where)->update($s);
             
                 $json['code']=$flag? 200 : 400;
                 $json['msg']=$flag? "修改成功" :"修改失败";

               return json($json);

            }
             
          }



           public function del(){
      if(IS_AJAX()){

      $s=input("param.");

      $where['id']=$s['id'];

      $del=Db::table('user_address')->where($where)->delete();

      $json["code"]=$del? 200: 400;

      $json["msg"]=$del?"删除成功":"删除失败";

        return json($json);
      }
    }




   
           public function collect(){
      
            $where['uid']=session('uid');
           
            $data=Db::table('user_collect')
                                            ->alias(['product' =>'p','user_collect'=>'c','user'=>'u'])
                                            ->field('c.id,u.Name as username,p.ProductCategory,c.time,c.title')
                                           ->join('user u','u.ID = c.uid')
                                           ->join('product p','p.ID = c.pid')
                                          ->where($where)
                                          ->select();
            $this->assign('data',$data);


            return view();

    }


        public function delcollect(){

         if(IS_AJAX()){
            $s=input('param.');

                $where['id']=$s['id'];
   // dump($where);
              $where['uid']=session('uid');

              $data=Db::table('user_collect')->where($where)->delete();

              $json['code']=$data ? 200 : 400 ;

              $json['msg']=$data ? "删除成功" : "删除失败" ;


              return json($json);
            }
          }




     public function mehemiao()
    {

        return view();
    }


     


   




    public function issueprice()
    {
     


        $where['AccountID']=session('uid');
        
        $price=Db::table('product')->alias('p')
                                   ->join('user u','u.ID = p.AccountID')
                                   ->field('u.Name as username,p.Name as pname,p.ProductCategory,p.CreateTime,p.State')
                                   ->where($where)
                                   ->select();
     

        $this->assign('price',$price);


         return view();
    }





     public function supplyissue()
    {


      

      if(IS_AJAX()){
        $s=input('param.');

      
       if(!$s['Name']||!$s['MinPrice']||!$s['MaxPrice']||!$s['unit']||$s['Mobile']!=11||!$s['DefaultImage']||!$s['Specification']||!$s['Pack']||!$s['Remarks']){
        $json['code']==400;
        $json['msg']=="内容填写完整";
       }
       $data['Name']=$s['Name'];
       $data['MinPrice']=$s['MinPrice'];
       $data['MaxPrice']=$s['MaxPrice'];
       $data['Unit'] = $s['unit'];
       $data['Mobile']=$s['Mobile'];
       $data['DefaultImage']=$s['DefaultImage'];
       $data['Specification']=$s['Specification'];
       $data['Pack']=$s['Pack'];
       $data['Remarks']=$s['Remarks'];


       
       $add=Db::table('service_supply')->insert($data);

            if($add){

                $json['code']=200;
                $json['url']=url('user/buyers/index');
  


            }else{

                $json['msg'] = "请输入正确的信息";
                $json['code'] = 400;

            }

                return json($json);

              }


        $Level['Level']=1;
       $data=Db::table('product_cate')->where($Level)->limit(10)->select();
  
        $this->assign('data',$data);

        


       

        $unit=Db::table('unit')->select();

       $this->assign('unit',$unit);

              
      return view();
    }




      // public function like(){

        
      //   // if($s['uid']){


      //   //   $where['uid']=$s['uid'];
      //   // }
      // }
   
    // public function like(){
    //   if(IS_AJAX()){
    //     $s=input('param.');
    //     if(!$s['title']){

    //       $json['code']=400;
    //       $json['msg']="请输入正确的标题";
    
    //     }else{
    //      $like['title']=$s['title'];
    //      $like['uid']=session('uid');
       

    //     $info=Db::table('so9_order')->where('uid','like','%' .$like['uid'].'%')
    //                                 ->where('title','like','%' .$like['title'].'%')
    //                                 ->select();



    //      $count = $info->where($like)->count();
    //     $Page       = new \Think\Page($count,2);
    //     // 实例化分页类 传入总记录数和每页显示的记录数(2)
    //     $show       = $Page->show();
    //     // 分页显示输出



          //  ->paginate(10);
          // $page = $info->render();
          //  $this->assign('page', $page);
      
          //   $this->assign('info',$info);




      //   if($info){
      //     $json['code']=200;
      //     $json['msg']="以显示相关的标题";

      //   }else{
      //     $json['code']=400;
      //     $json['msg']="请输入正确的标题";

      //   }

      //   }

      // return json($info);

      // }

      //   $like['id']=$s['id'];
      //   $data=Db::table('so9_order')->where($like)->order('id desc')->paginate(10);


     
    //    return view();

    // }
   







     //秒杀订单

    public function sec(){
      //全部

      // $s=input('param.');

      // if($s['title']){

      //   $where['title']=['like','%'.$s['title'].'%'];

      // }

        $s=input('param.');

       $s['uid']=session('uid');

        if($s['title']){

          $where['title']=['like','%'.$s['title'].'%'];
          
        }

        if($s['uid']){

          $where['uid']=$s['uid'];
        }
         $sec = Db::table('so9_order')->where($where)->order('ID desc')->paginate(5);

        // 获取分页显示
        $page = $sec->render();
        $this->assign('sec', $sec);
        $this->assign('page', $page);
        // return view();


  

    // $where['uid']=session('uid');
    // $where['type']='ms';

    // $sec=Db::table('so9_order')->where($where)->paginate(5);
  

        // 获取分页显示
      
   
   //已付款
    $where['state']=1;
    $state=Db::table('so9_order')->where($where)->select();
    $this->assign('state',$state);

    //已发货
    $where['state']=2;
    $deliver=Db::table('so9_order')->where($where)->select();
    $this->assign('deliver',$deliver);

    //已完成
    $where['state']=3;
    $end=Db::table('so9_order')->where($where)->select();
    $this->assign('end',$end);



    //退款中
     $where['state']=4;
    $money=Db::table('so9_order')->where($where)->select();
    $this->assign('money',$money);

    // dump($sec);

    //   $page = $sec->render();
    // $this->assign('page', $page);
      
    // $this->assign('sec',$sec);

        return view();

    }


    public function delsec(){
      if(IS_AJAX()){

      $s=input("param.");

      $where['id']=$s['id'];

      $del=Db::table('so9_order')->where($where)->delete();

      $json["code"]=$del? 200: 400;

      $json["msg"]=$del?"删除成功":"删除失败";

        return json($json);
      }
    }




    //拼团订单
    public function spell(){
      $where['type']='pt';
      $where['uid']=session('uid');

      $spell=Db::table('so9_order')->where($where)->paginate(1);

      $this->assign('spell',$spell);

   //已付款
    $where['state']=1;
    $state=Db::table('so9_order')->where($where)->select();
    $this->assign('state',$state);


    //已发货
    $where['state']=2;
    $deliver=Db::table('so9_order')->where($where)->select();
    $this->assign('deliver',$deliver);

    //已完成
    $where['state']=3;
    $end=Db::table('so9_order')->where($where)->select();
    $this->assign('end',$end);



    //退款中
     $where['state']=4;
    $money=Db::table('so9_order')->where($where)->select();
    $this->assign('money',$money);

     $page = $spell->render();
    $this->assign('page', $page);
      
    $this->assign('spell',$spell);



        return view();
    }

    //集团订单
    public function bloc(){
      $where['AccountID']=session('uid');

      $bloc=Db::table('service_company')->where($where)->paginate(1);

      $this->assign('bloc',$bloc);



      //已付款
    $where['State']=1;
    $state=Db::table('service_company')->where($where)->select();
    $this->assign('state',$state);

    //已发货
    $where['State']=2;
    $deliver=Db::table('service_company')->where($where)->select();
    $this->assign('deliver',$deliver);

    //已完成
    $where['State']=3;
    $end=Db::table('service_company')->where($where)->select();
    $this->assign('end',$end);



    //退款中
     $where['State']=4;
    $money=Db::table('service_company')->where($where)->select();
    $this->assign('money',$money);

    $page = $bloc->render();
    $this->assign('page', $page);
      
    $this->assign('bloc',$bloc);

      // dump($bloc);

        return view();

    }


    //大宗定制订单
    public function large(){
        $where['AccountID']=session('uid');

        $custom=Db::table('service_custom')->where($where)->paginate(1);

        $this->assign('custom',$custom);
        // dump($custom);


        $where['State']=1;

        $data=Db::table('service_custom')->where($where)->select();

        $this->assign('data',$data);

          //已发货
    $where['State']=2;
    $deliver=Db::table('service_custom')->where($where)->select();
    $this->assign('deliver',$deliver);

    //已完成
    $where['State']=3;
    $end=Db::table('service_custom')->where($where)->select();
    $this->assign('end',$end);



    //退款中
     $where['State']=4;
    $money=Db::table('service_custom')->where($where)->select();
    $this->assign('money',$money);


    $page = $custom->render();

    $this->assign('page', $page);
      
    $this->assign('custom',$custom);
      

        return view();
    }



     public function dellarge(){
      if(IS_AJAX()){

      $s=input("param.");

      $where['id']=$s['id'];

      $del=Db::table('service_custom')->where($where)->delete();

      $json["code"]=$del? 200: 400;

      $json["msg"]=$del?"删除成功":"删除失败";

        return json($json);
      }
    }







  




     }  